import threading
import time


def w1(n: list):
    n.append(44)
    print("--in w1--", n)


def w2(n: list):
    time.sleep(1)
    print("--in w2--", n)


# g_n = [11,22,33]
# t1 = threading.Thread(target=w1, args=(g_n,))
# t1.start()
# t2 = threading.Thread(target=w2, args=(g_n,))
# t2.start()

# 共享的全局变量
n = 0
# 互斥锁
l = threading.Lock()

def s1():
    global n
    # 上锁
    l.acquire()
    for i in range(1000000):
        n += 1
    print("--in s1--:", n)
    # 释放锁
    l.release()


def s2():
    l.acquire()
    global n
    for i in range(1000000):
        n += 1
    print("--in s2--:", n)
    l.release()


if __name__ == '__main__':
    t1 = threading.Thread(target=s1)
    t1.start()
    t2 = threading.Thread(target=s2)
    t2.start()
